﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DLV.Utilities;
using DLV.Library.Utilities;

namespace DLV.Data.Entities
{
    public class LocationController
    {
        public int InsertLocation(LocationInfo info)
        {
            return SQL.InsertLocation(info.LocationName, info.LocationTypeId, info.SortDescription, info.DetailDescription, info.ContinentId, info.NationalId, info.CityId, info.DistrictId, info.WardId, info.Address, info.CreatedBy, info.CreatedDate, info.UpdatedBy, info.UpdatedDate, info.Status, info.Code);
        }
        public void UpdateLocation(LocationInfo info)
        {
            SQL.UpdateLocation(info.LocationId, info.LocationName, info.LocationTypeId, info.SortDescription, info.DetailDescription, info.ContinentId, info.NationalId, info.CityId, info.DistrictId, info.WardId, info.Address, info.CreatedBy, info.CreatedDate, info.UpdatedBy, info.UpdatedDate, info.Status, info.Code);
        }
        public void DeleteLocation(LocationInfo info)
        {
            DeleteLocation(info.LocationId);
        }
        public void DeleteLocation(int locationId)
        {
            SQL.DeleteLocation(locationId);
        }
        public LocationInfo GetLocation(int locationId)
        {
            return CBO.FillObject<LocationInfo>(SQL.GetLocation(locationId));
        }
        public List<LocationInfo> ListLocation()
        {
            return CBO.FillCollection<LocationInfo>(SQL.ListLocation());
        }
        public List<LocationInfo> ListLocationByStatus(int status)
        {
            return ListLocation().Where(p=>p.Status==status).ToList();
        }
        public List<LocationInfo> ListLocationByScheduleType(int scheduleType)
        {
            //scheduleType: 1-lịch trình bay, 2-lịch trình tàu hỏa, 3-lịch trình tàu thủy, status: 1-sử dụng
            if (scheduleType == 1)
            {
                return ListLocation().Where(p => p.LocationTypeId == 1 && p.Status==1).ToList();
            }
            else if (scheduleType == 2)
            {
                return ListLocation().Where(p => p.LocationTypeId == 11 && p.Status == 1).ToList();
            }
            else if (scheduleType == 3)
            {
                return ListLocation().Where(p => p.LocationTypeId == 10 && p.Status == 1).ToList();
            }
            else
            {
                return ListLocation().Where(p=>p.Status==1).ToList();
            }
        }

        public List<LocationInfo> ListLocationByLocationType(int locationType)
        {
            return ListLocation().Where(p => p.LocationTypeId == locationType).ToList();
        }
        public DataTable SelectLocation()
        {
            return CBO.ConvertToDataTable(ListLocation(), typeof(LocationInfo));
        }

        public Dictionary<object, object> ListLocationLookup()
        {
            var locations = ListLocationByStatus((int)Globals.DocumentStatus.Use).OrderBy(p => p.LocationName);
            return locations.ToDictionary<LocationInfo, object, object>(info => info.LocationId,info => info.LocationName);
        }
    }
}
